package com.itheima.studentmanager;

import com.itheima.domain.Student;

import java.util.ArrayList;

public class StudentDao {
    static ArrayList<Student> list = new ArrayList<>();

    public void add(Student stu) {
        list.add(stu);
    }

    public ArrayList<Student> findAll() {
        return list;
    }

    public void delete(int id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (id == stu.getId()) {
                list.remove(i);
            }
        }
    }

    public Student findOne(int id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (id == stu.getId()) {
                return stu;
            }
        }
        return null;
    }

    public void update(int id, Student student) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (id == stu.getId()) {
                list.set(i, student);
            }
        }
    }
}
